import QtQuick 2.12
import Qt.labs.folderlistmodel 2.12
import QtQuick.Layouts 1.12

Rectangle {
    id: container
    property var listCurrentIndex: -1
    property var firendId: 1
    property var heads

    // 聊天窗口视图：后面再做，先放这里
    ChatList{
        id: chat
        width: parent.width
        height: parent.height
        opacity: 0
        enabled: opacity == 1.0     // 只有当显示的时候，才接收鼠标键盘事件
        onExitRequest: exitAnim.start()
        function show(){
            scale = 1
            showAnim.start()
        }

        // 显示动画：
        ParallelAnimation{
            id:showAnim
            NumberAnimation {
                target: recentFirendList
                property: "opacity"
                to: 0
                duration: 300
                easing.type: Easing.InOutQuad
            }
            NumberAnimation {
                target: recentFirendList
                property: "x"
                to: -parent.width
                duration: 300
                easing.type: Easing.InOutQuad
            }
            NumberAnimation {
                target: chat
                property: "x"
                duration: 200
                from: parent.width
                to: 0
                easing.type: Easing.InOutQuad
            }
            NumberAnimation {
                target: chat
                property: "opacity"
                duration: 200
                from: 0
                to: 1
                easing.type: Easing.InOutQuad
            }
        }
        // 退出动画:
        SequentialAnimation {
            id: exitAnim

            ParallelAnimation {

                NumberAnimation {
                    target: recentFirendList
                    property: "opacity"
                    to: 1.0
                    duration: 200
                }
                NumberAnimation {
                    target: recentFirendList
                    property: "x"
                    from: -parent.width
                    to: 0
                    duration: 200
                }
                NumberAnimation {
                    target: chat
                    property: "x"
                    to: chat.width
                    duration: 250
                }
                NumberAnimation {
                    target: chat
                    property: "opacity"
                    from: 1.0
                    to: 0
                    duration: 200
                }
            }
        }
    }

    // 主界面朋友列表布局
    ColumnLayout{
        id:recentFirendList
        width: parent.width
        height: parent.height

        // 主界面工具栏
        RecentFirendTopBar{
            Layout.fillWidth: true
            z:2
            onNewFirend: container.addExample()
        }

        ListView{
            id:_view
            Layout.fillHeight: true
            Layout.fillWidth: true
            clip: true

            // 定义delegate
            delegate: RecentFirendDelegate{
                //currentIndex: listCurrentIndex      // 传递是否选中
                onCurrentIndexChange: listCurrentIndex = index
                onClicked:{
                    chat.chatData = myModel.get(index)
                    chat.show()
                    //console.log("点击了第",index,"个用户")
                    //console.log( myModel.get(index).recode.get(0).msg)
                }
                viewMoving:_view.moving
                onRemoved: myModel.remove(index)
            }

            // 定义模型
            model: ListModel {
                id: myModel
            }

            // 由于model全是通过一个个添加的,所以不会触发populate属性
            add: Transition {
                      NumberAnimation { properties: "scale";  from: 0; to: 1.0; duration: 90 }
            }
            displaced: Transition {
                      NumberAnimation { properties: "x,y"; duration: 500 }
            }

        }


    }

    // 组件加载以后，自动添加10个：
    Component.onCompleted: {
        heads =  [
            "qrc:/head/head1.jpg",
            "qrc:/head/head2.jpg",
            "qrc:/head/head3.jpg",
            "qrc:/head/head4.jpg",
            "qrc:/head/head5.jpg",
            "qrc:/head/head6.jpg",
            "qrc:/head/head7.jpg",
            "qrc:/head/head8.jpg",
            "qrc:/head/head9.jpg",
            "qrc:/head/head10.jpg",
            "qrc:/head/head11.jpg",
            "qrc:/head/head12.jpg",
            "qrc:/head/head13.jpg",
        ];

        // 添加10个样例
        var date = new Date();
        for(var i=0 ; i<10 ; i++ ){
            appendExample(date);
            date = new Date(date.getFullYear(),date.getMonth(),date.getDate()-1,    // 年月日
                            parseInt(Math.random()*24),     // 小时
                            parseInt(Math.random()*60),     // 分钟
                            00,00                           // 格式
                            )
        }
    }



    // 添加前10个测试样例：
    function appendExample(dateArg){
        myModel.append(
                    {
                        'name':"测试-小号" + firendId.toString(),
                        'headSrc':heads[parseInt(Math.random()*heads.length)],
                        'recode':[
                            {
                                date:dateArg,
                                msgType:"hint",
                                msg:"你已添加" + "测试-小号" + firendId.toString() + "现在可以开始聊天了。"
                            },
                            {
                                date:dateArg,
                                msgType:"hintDate",
                                msg:"",
                            },
                            {
                                date:dateArg,
                                msgType:'recv',
                                msg:'你好',
                            }
                        ]
                    });
        firendId += 1
    }

    // 点击 + 号以后添加的：
    function addExample(){
        myModel.insert(0,
                    {
                        'name':"测试-小号" + firendId.toString(),
                        'headSrc':heads[parseInt(Math.random()*heads.length)],
                        'recode':[
                            {
                                date:new Date(),
                                msgType:"hint",
                                msg:"你已添加" + "测试-小号" + firendId.toString() + "现在可以开始聊天了。"
                            },
                        ]
                    });
            firendId += 1
    }


}
